
package pkg_group5;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author Stephen Moulton
 */

/* Works out if the Payment details are vaild.
 *
 */
@WebService(name = "Payment", serviceName = "PaymentService")
public class Payment
{
    @WebMethod(operationName = "authorizePayment")
    public String[] authorizePayment(@WebParam(name = "firstName") String firstName,@WebParam(name = "surname") String surname,@WebParam(name = "expiryDate") String expiryDate,@WebParam(name = "cardType") String cardType,
            @WebParam(name = "securityNum")int securityNum,@WebParam(name = "cardNum") long cardNum) throws ParseException, Exception
    {
        String[] results = new String[6];
        int count = 1;

        if(validateCardNumber(cardNum,cardType).equalsIgnoreCase("Failed"))
        {
          results[count] = "Card type or Card number incorrect";
          count++;
        }

        if(validateExpiryDate(expiryDate).equalsIgnoreCase("Failed"))
        {
           results[count] = "Card expired";
           count++;
        }

        if(validateFName(firstName).equalsIgnoreCase("Failed"))
        {
           results[count] = "First name incorrect";
           count++;
        }
        if(validateFName(surname).equalsIgnoreCase("Failed"))
        {
            results[count] = "Surname incorrect";
            count++;
        }

        if(validateSecurityNum(securityNum).equalsIgnoreCase("Failed"))
        {
            results[count] = "Security number incorrect";
            count++;
        }

        if(count > 1)
        {
            results[0] = "Failed";
        }
        else
        {
             results[0] = "Success";
        }
       return results;
    }

    private String validateCardNumber(long cNumber, String type)
    {
        String tmp = Long.toString(cNumber);
        int length = tmp.length();

        if(type.equalsIgnoreCase("American Express"))
        {
            if(length == 15)
                return "Success";
        }
        else if(type.equalsIgnoreCase("Visa") || type.equalsIgnoreCase("Visa Electron") || type.equalsIgnoreCase("MasterCard")
                || type.equalsIgnoreCase("Discover Card"))
        {
            if(length == 16)
                return "Success";
        }
        else if(type.equalsIgnoreCase("Solo") || type.equalsIgnoreCase("Switch"))
        {
            if(length >= 16 && length <= 19)
                return "Success";
        }
        else if(type.equalsIgnoreCase("Maestro"))
        {
            if(length >= 12 && length <= 19)
                return "Success";
        }
        return "Failed";
    }

     private static String validateExpiryDate(String strDate) throws ParseException
    {
        String result = null;
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        Date date = (Date)formatter.parse(strDate);
        Calendar cal = Calendar.getInstance();
        Calendar currentcal = Calendar.getInstance();
        cal.setTime(date);
        currentcal.set(currentcal.get(Calendar.YEAR),currentcal.get(Calendar.MONTH), currentcal.get(Calendar.DAY_OF_MONTH));

        if(cal.before(currentcal))
            result = "Failed";
        else if(cal.after(currentcal))
            result = "Success";
        else
            result = "Failed";

        return result;
    }

    private String validateSecurityNum(int sNumber)
    {
        //Test that the length is only three
        String tmp = Integer.toString(sNumber);
        int length = tmp.length();
        if(length == 3)
            return "Success";

        return "Failed";
    }

    private String validateFName(String fname)
    {
        //Check that there is only letters in the string
        if(fname.matches("^[a-zA-Z]+$"))
        {
            return "Success";
        }
        return "Failed";
    }

    private String validateSName(String sname)
    {
       //Check that there is only letters in the string
       if(sname.matches("^[a-zA-Z]+$"))
        {
            return "Success";
        }
        return "Failed";
    }
}
